#include "BlockQueue.hpp"
#include <memory>
#include <cstdlib>
#include <unistd.h>

const int cNums = 5; //消费者数量
const int pNums = 3; //生产者数量

void *consume(void *args)
{
    BlockQueue<int> *q = static_cast<BlockQueue<int> *>(args);

    while (true)
    {
        int data;

        //消费了一个数据
        q->pop(&data);

        // sleep(1);
    }
}

void *produce(void *args)
{
    BlockQueue<int> *q = static_cast<BlockQueue<int> *>(args);

    while (true)
    {
        int data = rand() % 10;
        //模拟生产数据耗费的时间
        usleep(10);

        q->push(data);

        // sleep(1);
    }
}

int main()
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<int> *queue = new BlockQueue<int>;

    pthread_t p[pNums], c[cNums];
    for (int i = 0; i < pNums; i++)
    {
        pthread_create(p + i, nullptr, consume, queue);
    }

    for (int i = 0; i < cNums; i++)
    {
        pthread_create(c + i, nullptr, produce, queue);
    }

    for (int i = 0; i < pNums; i++)
    {
        pthread_join(p[i], nullptr);
    }

    for (int i = 0; i < cNums; i++)
    {
        pthread_join(c[i], nullptr);
    }


    delete queue;
    return 0;
}